import HFRequest from "./index";
import router from "@/router/index";
import { appConfig } from "../../appConfig/index";

export const hfoRequest = new HFRequest(
  {
    baseURL: appConfig[import.meta.env.MODE as IEnvOType].AXIOS_BASEURL,
    interceptors: {
      requestInterceptor: (config) => {
        return config;
      },
      requestInterceptorCatch: (err) => {
        return err;
      },
      responseInterceptor: (res) => {
        return res.data
      },
      responseInterceptorCatch: (error) => {
        let res: Error | undefined;
        ElMessage.error(error.response.data.message);
        if ([401, 403].includes(error.response.status)) {
          router.push("/login");
          throw res;
        }
        if (res instanceof Error) throw res;
        return error.response.data
      }
    }
  },
  (config) => {
    const useStore = useAllStore();
    if (useStore.token && config.url) {
      config.headers = {};
      config.headers.Authorization = useStore.token;
    }
  }
);
